<?php

App::uses('AppController', 'Controller');

/**
 * subclaselaboratorio Controller
 *
 * @property Subclaseslaboratorio $sbcl
 */
class SubclaseslaboratoriosController extends AppController {

    public $layout = 'hospital';
    public $uses = array('Subclaseslaboratorio','Claseslaboratorio');
    /**
     * index method
     *
     * @return void
     */

    public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow();
    }

    public function index() {
       $sub = $this->Subclaseslaboratorio->find('all', array('recursive'=>1));
        $this->set(compact('sub'));
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Subclaseslaboratorio->exists($id)) {
            throw new NotFoundException(__('Invalid subclaseslaboratorio'));
        }
        $options = array('conditions' => array('Subclaseslaboratorio' . $this->Subclaseslaboratorio->primaryKey => $id));
        $this->set('subclaseslaboratorio', $this->Subclaseslaboratorio->find('first', $options));
    }

    /**s
     * add method
     *
     * @return void
     */
    public function add() {

        if ($this->request->is('post')) {

            //debug($this->request->data); exit();
           $idlabo= $this->request->data['Subclaseslaboratorio']['claseslaboratorio_id'];
           //debug($idlabo); exit();

             $maxid = $this->Subclaseslaboratorio->find('count', array('conditions' =>            
                array('Claseslaboratorio.id' => $idlabo)));

             $this->request->data['Subclaseslaboratorio']['posicion'] = $maxid+1;



            $this->Subclaseslaboratorio->create();

            if ($this->Subclaseslaboratorio->save($this->request->data)) {
                $this->Session->setFlash(__('The subclaseslaboratorio has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The subclaseslaboratorio could not be saved. Please, try again.'));
            }
        }
        else{

            $claseslaboratorio = $this->Claseslaboratorio->find('all',
                    array('fields'=>array('Claseslaboratorio.id','Claseslaboratorio.nombre'),
                        'recursive'=>-1
                        ));

            $this->set(compact('claseslaboratorio'));


        }
        }
        //$parametros = $this->Parametro->find('list');
        //$this->set(compact('parametros'));
    

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {

        if (!$this->Subclaseslaboratorio->exists($id)) {
            throw new NotFoundException(__('Invalid subclaseslaboratorio'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            //debug($this->request->data());exit;
            $this->Subclaseslaboratorio->id = $id;
            if ($this->Subclaseslaboratorio->save($this->request->data)) {
                $this->Session->setFlash(__('The subclaseslaboratorio has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The subclaseslaboratorio could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Subclaseslaboratorio.' . $this->Subclaseslaboratorio->primaryKey => $id));
            $this->request->data = $this->Subclaseslaboratorio->find('first', $options);
        }
        $claseslaboratorio=$this->Claseslaboratorio->find('all',array('recursive'=>-1));
        //$parametros = $this->Parametro->find('list');
        $this->set(compact('claseslaboratorio'));
    }


    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Subclaseslaboratorio->id = $id;
        if (!$this->Subclaseslaboratorio->exists()) {
            throw new NotFoundException(__('Invalid subclaseslaboratorio'));
        }
        //$this->request->onlyAllow('post', 'delete');
        if ($this->Subclaseslaboratorio->delete()) {
            $this->Session->setFlash(__('Subclaseslaboratorio deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('subclaseslaboratorio was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
}
